clear all
set more off
capture log close
log using "$output/figure9.log", replace

use "$data/acsmergeddata.dta", clear

*** total adults
gen pop=(perwt + spouseperwt)
egen totadults=sum(pop)
di totadults
drop pop

gen pop=(newnumkidswt17)
egen totkids=sum(pop)
di totkids
drop pop

** total effect on poverty
gen pooradults=(age>=18)*poor*(perwt+spouseperwt)
egen totpooradults=sum(pooradults)

gen nolongerpooradults=(age>=18)*poor*(poorctc21==0)*(perwt+spouseperwt)
egen totnolongerpooradults=sum(nolongerpooradults)

gen stillpooradults=(age>=18)*poor*(poorctc21==1)*(perwt+spouseperwt)
egen totstillpooradults=sum(stillpooradults)

drop stillpoor totstillpoor 

gen poorkids=poor*(newnumkidswt17)
egen totpoorkids=sum(poorkids)
di totpoorkids

gen stillpoor=poor*(poorctc21==1)*(newnumkidswt17)
egen totstillpoor=sum(stillpoor)

di totkids
di totpoorkids
di totstillpoor

di totpoorkids/totkids
di (totstillpoor)/totkids

** VARIOUS ELASTICITIES FOR UNMARRIED MOTHERS

forvalues elas=0(1)20{
preserve

* row 1
gen double group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*(`elas'/10)*group
egen totgroup1a=sum(group1)
gen double group2=newnumkidswt17*avgtax*(`elas'/10)*group*poor
egen totgroup2a=sum(group2)

gen double group3=newnumkidswt17*avgtax*(`elas'/10)*group*(poor==0)
egen totgroup3a=sum(group3)

gen double group4=newnumkidswt17*avgtax*(`elas'/10)*group*(poor==0)*poorctc21nother
egen totgroup4a=sum(group4)

gen double group5=newnumkidswt17*avgtax*(`elas'/10)*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5a=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'a=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'a/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 2
gen double group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.2*group
egen totgroup1b=sum(group1)

gen double group2=newnumkidswt17*avgtax*.2*group*poor
egen totgroup2b=sum(group2)

gen double group3=newnumkidswt17*avgtax*.2*group*(poor==0)
egen totgroup3b=sum(group3)

gen double group4=newnumkidswt17*avgtax*.2*group*(poor==0)*poorctc21nother
egen totgroup4b=sum(group4)

gen double group5=newnumkidswt17*avgtax*.2*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5b=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'b=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'b/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 3
gen double group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.2*group
egen totgroup1c=sum(group1)

gen double group2=newnumkidswt17*avgtax*.2*group*poor
egen totgroup2c=sum(group2)

gen double group3=newnumkidswt17*avgtax*.2*group*(poor==0)
egen totgroup3c=sum(group3)

gen double group4=newnumkidswt17*avgtax*.2*group*(poor==0)*poorctc21nother
egen totgroup4c=sum(group4)

gen double group5=newnumkidswt17*avgtax*.2*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5c=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'c=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'c/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 4
gen double group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*newrtwchangespouse*.05*group
egen totgroup1d=sum(group1)

gen double group2=newnumkidswt17*newrtwchangespouse*.05*group*poor
egen totgroup2d=sum(group2)

gen double group3=newnumkidswt17*newrtwchangespouse*.05*group*(poor==0)
egen totgroup3d=sum(group3)

gen double group4=newnumkidswt17*newrtwchangespouse*.05*group*(poor==0)*poorctc21notspouse
egen totgroup4d=sum(group4)

gen double group5=newnumkidswt17*newrtwchangespouse*.05*group*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5d=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'd=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'd/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)

gen double group1=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.05
egen totgroup1e=sum(group1)

gen double group2=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.05*poor
egen totgroup2e=sum(group2)

gen double group3=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.05*(poor==0)
egen totgroup3e=sum(group3)

gen double group4=(newnumkidswt17*person1)*avgtax*.05*(poor==0)*poorctc21nother+(newnumkidswt17*person2)*avgtax*.05*(poor==0)*poorctc21notspouse

egen totgroup4e=sum(group4)

gen double group5=(newnumkidswt17*person1)*avgtax*.05*(poor==1)*(poorctc21==0)*poorctc21nother+newnumkidswt17*person2*avgtax*.05*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5e=sum(group5)

gen pov1=(poor*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov2=(poorctc21*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov3=(poorctc21*(person1*newnumkidswt17+person2*newnumkidswt17)+group4+group5)

egen pop=sum(newnumkidswt17*person1+newnumkidswt17*person2)
forvalues i=1/3{
	egen povnum`i'e=sum(pov`i')
	gen povrate`i'=povnum`i'e/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

forvalues i=1/5{
	di totgroup`i'a+totgroup`i'b+totgroup`i'c+totgroup`i'd+totgroup`i'e
}

di totkids
di totpoorkids
di totstillpoor
di (totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
di totpoorkids/totkids
di totstillpoor/totkids
di (totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)/totkids
di "pov % decrease="
di (totpoorkids-(totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e))/totpoorkids

local netkidpovrate_e`elas'=(totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
local kidpovchange_e`elas'=(totpoorkids-(totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e))/totpoorkids

local lesspoorkids_e`elas'=totpoorkids-(totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)

di `lesspoorkids_e`elas''

local employment_e`elas'=totgroup1a+totgroup1b+totgroup1c+totgroup1d+totgroup1e
di `employment_e`elas''

di `netkidpovrate_e`elas'' 
di `kidpovchange_e`elas''
restore

}

gen elasticity=.
gen netkidpovrate1=.
gen kidpovchange1=.
gen lesspoorkids1=.
gen employment1=.

forvalues elas=0(1)20{
	replace elasticity=`elas' if _n==`elas'+1
	replace netkidpovrate1=`netkidpovrate_e`elas''/1000000 if _n==`elas'+1
	replace kidpovchange1=`kidpovchange_e`elas'' if _n==`elas'+1
	replace lesspoorkids1=`lesspoorkids_e`elas'' if _n==`elas'+1
	replace employment1=`employment_e`elas'' if _n==`elas'+1
	}

* EVERYONE ELSE GETS 0.25

forvalues elas=0(1)20{
preserve

* row 1
gen double group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*(`elas'/10)*group
egen totgroup1a=sum(group1)
gen double group2=newnumkidswt17*avgtax*(`elas'/10)*group*poor
egen totgroup2a=sum(group2)

gen double group3=newnumkidswt17*avgtax*(`elas'/10)*group*(poor==0)
egen totgroup3a=sum(group3)

gen double group4=newnumkidswt17*avgtax*(`elas'/10)*group*(poor==0)*poorctc21nother
egen totgroup4a=sum(group4)

gen double group5=newnumkidswt17*avgtax*(`elas'/10)*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5a=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'a=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'a/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 2
gen double group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.25*group
egen totgroup1b=sum(group1)

gen double group2=newnumkidswt17*avgtax*.25*group*poor
egen totgroup2b=sum(group2)

gen double group3=newnumkidswt17*avgtax*.25*group*(poor==0)
egen totgroup3b=sum(group3)

gen double group4=newnumkidswt17*avgtax*.25*group*(poor==0)*poorctc21nother
egen totgroup4b=sum(group4)

gen double group5=newnumkidswt17*avgtax*.25*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5b=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'b=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'b/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 3
gen double group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.25*group
egen totgroup1c=sum(group1)

gen double group2=newnumkidswt17*avgtax*.25*group*poor
egen totgroup2c=sum(group2)

gen double group3=newnumkidswt17*avgtax*.25*group*(poor==0)
egen totgroup3c=sum(group3)

gen double group4=newnumkidswt17*avgtax*.25*group*(poor==0)*poorctc21nother
egen totgroup4c=sum(group4)

gen double group5=newnumkidswt17*avgtax*.25*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5c=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'c=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'c/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 4
gen double group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*newrtwchangespouse*.25*group
egen totgroup1d=sum(group1)

gen double group2=newnumkidswt17*newrtwchangespouse*.25*group*poor
egen totgroup2d=sum(group2)

gen double group3=newnumkidswt17*newrtwchangespouse*.25*group*(poor==0)
egen totgroup3d=sum(group3)

gen double group4=newnumkidswt17*newrtwchangespouse*.25*group*(poor==0)*poorctc21notspouse
egen totgroup4d=sum(group4)

gen double group5=newnumkidswt17*newrtwchangespouse*.25*group*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5d=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'd=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'd/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)

gen double group1=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25
egen totgroup1e=sum(group1)

gen double group2=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25*poor
egen totgroup2e=sum(group2)

gen double group3=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25*(poor==0)
egen totgroup3e=sum(group3)

gen double group4=(newnumkidswt17*person1)*avgtax*.25*(poor==0)*poorctc21nother+(newnumkidswt17*person2)*avgtax*.25*(poor==0)*poorctc21notspouse

egen totgroup4e=sum(group4)

gen double group5=(newnumkidswt17*person1)*avgtax*.25*(poor==1)*(poorctc21==0)*poorctc21nother+newnumkidswt17*person2*avgtax*.25*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5e=sum(group5)

gen pov1=(poor*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov2=(poorctc21*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov3=(poorctc21*(person1*newnumkidswt17+person2*newnumkidswt17)+group4+group5)

egen pop=sum(newnumkidswt17*person1+newnumkidswt17*person2)
forvalues i=1/3{
	egen povnum`i'e=sum(pov`i')
	gen povrate`i'=povnum`i'e/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

forvalues i=1/5{
	di totgroup`i'a+totgroup`i'b+totgroup`i'c+totgroup`i'd+totgroup`i'e
}

di totkids
di totpoorkids
di totstillpoor
di (totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
di totpoorkids/totkids
di totstillpoor/totkids
di (totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)/totkids
di "pov % decrease="
di (totpoorkids-(totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e))/totpoorkids

local netkidpovrate_e`elas'=(totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
local kidpovchange_e`elas'=(totpoorkids-(totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e))/totpoorkids

local lesspoorkids_e`elas'=totpoorkids-(totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)

di `lesspoorkids_e`elas''

local employment_e`elas'=totgroup1a+totgroup1b+totgroup1c+totgroup1d+totgroup1e
di `employment_e`elas''

di `netkidpovrate_e`elas'' 
di `kidpovchange_e`elas''
restore

}

gen netkidpovrate2=.
gen kidpovchange2=.
replace elasticity=elasticity/10

gen lesspoorkids2=.
gen employment2=.

forvalues elas=0(1)20{
	*replace elasticity=`elas' if _n==`elas'+1
	replace netkidpovrate2=`netkidpovrate_e`elas''/1000000 if _n==`elas'+1
	replace kidpovchange2=`kidpovchange_e`elas'' if _n==`elas'+1
	
	replace lesspoorkids2=`lesspoorkids_e`elas'' if _n==`elas'+1
	replace employment2=`employment_e`elas'' if _n==`elas'+1
	}

**************************************************
* DEEP POVERTY 
**************************************************

gen deeppoorkids=deeppoor*(newnumkidswt17)
egen totdeeppoorkids=sum(deeppoorkids)
di totdeeppoorkids

gen stilldeeppoor=deeppoor*(deeppoorctc21==1)*(newnumkidswt17)
egen totstilldeeppoor=sum(stilldeeppoor)

di totkids
di totdeeppoorkids
di totstilldeeppoor

di totdeeppoorkids/totkids
di (totstilldeeppoor)/totkids

** VARIOUS ELASTICITIES FOR UNMARRIED MOTHERS

forvalues elas=0(1)20{
preserve

* row 1
gen double group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*(`elas'/10)*group
egen totgroup1a=sum(group1)
gen double group2=newnumkidswt17*avgtax*(`elas'/10)*group*deeppoor
egen totgroup2a=sum(group2)

gen double group3=newnumkidswt17*avgtax*(`elas'/10)*group*(deeppoor==0)
egen totgroup3a=sum(group3)

gen double group4=newnumkidswt17*avgtax*(`elas'/10)*group*(deeppoor==0)*deeppoorctc21nother
egen totgroup4a=sum(group4)

gen double group5=newnumkidswt17*avgtax*(`elas'/10)*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother
egen totgroup5a=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'a=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'a/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 2
gen double group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.2*group
egen totgroup1b=sum(group1)

gen double group2=newnumkidswt17*avgtax*.2*group*deeppoor
egen totgroup2b=sum(group2)

gen double group3=newnumkidswt17*avgtax*.2*group*(deeppoor==0)
egen totgroup3b=sum(group3)

gen double group4=newnumkidswt17*avgtax*.2*group*(deeppoor==0)*deeppoorctc21nother
egen totgroup4b=sum(group4)

gen double group5=newnumkidswt17*avgtax*.2*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother
egen totgroup5b=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'b=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'b/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 3
gen double group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.2*group
egen totgroup1c=sum(group1)

gen double group2=newnumkidswt17*avgtax*.2*group*deeppoor
egen totgroup2c=sum(group2)

gen double group3=newnumkidswt17*avgtax*.2*group*(deeppoor==0)
egen totgroup3c=sum(group3)

gen double group4=newnumkidswt17*avgtax*.2*group*(deeppoor==0)*deeppoorctc21nother
egen totgroup4c=sum(group4)

gen double group5=newnumkidswt17*avgtax*.2*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother
egen totgroup5c=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'c=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'c/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 4
gen double group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*newrtwchangespouse*.05*group
egen totgroup1d=sum(group1)

gen double group2=newnumkidswt17*newrtwchangespouse*.05*group*deeppoor
egen totgroup2d=sum(group2)

gen double group3=newnumkidswt17*newrtwchangespouse*.05*group*(deeppoor==0)
egen totgroup3d=sum(group3)

gen double group4=newnumkidswt17*newrtwchangespouse*.05*group*(deeppoor==0)*deeppoorctc21notspouse
egen totgroup4d=sum(group4)

gen double group5=newnumkidswt17*newrtwchangespouse*.05*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21notspouse
egen totgroup5d=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'd=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'd/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)

gen double group1=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.05
egen totgroup1e=sum(group1)

gen double group2=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.05*deeppoor
egen totgroup2e=sum(group2)

gen double group3=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.05*(deeppoor==0)
egen totgroup3e=sum(group3)

gen double group4=(newnumkidswt17*person1)*avgtax*.05*(deeppoor==0)*deeppoorctc21nother+(newnumkidswt17*person2)*avgtax*.05*(deeppoor==0)*deeppoorctc21notspouse

egen totgroup4e=sum(group4)

gen double group5=(newnumkidswt17*person1)*avgtax*.05*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother+newnumkidswt17*person2*avgtax*.05*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21notspouse
egen totgroup5e=sum(group5)

gen pov1=(deeppoor*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov2=(deeppoorctc21*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov3=(deeppoorctc21*(person1*newnumkidswt17+person2*newnumkidswt17)+group4+group5)

egen pop=sum(newnumkidswt17*person1+newnumkidswt17*person2)
forvalues i=1/3{
	egen povnum`i'e=sum(pov`i')
	gen povrate`i'=povnum`i'e/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

forvalues i=1/5{
	di totgroup`i'a+totgroup`i'b+totgroup`i'c+totgroup`i'd+totgroup`i'e
}

di totkids
di totdeeppoorkids
di totstilldeeppoor
di (totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
di totdeeppoorkids/totkids
di totstilldeeppoor/totkids
di (totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)/totkids
di "pov % decrease="
di (totdeeppoorkids-(totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e))/totdeeppoorkids

local netkidpovrate_e`elas'=(totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
local kidpovchange_e`elas'=(totdeeppoorkids-(totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e))/totdeeppoorkids

local lessdeeppoorkids_e`elas'=totdeeppoorkids-(totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
di `lessdeeppoorkids_e`elas''

di `netkidpovrate_e`elas'' 
di `kidpovchange_e`elas''
restore

}

gen netkiddeeppovrate1=.
gen kiddeeppovchange1=.

gen lessdeeppoorkids1=.

forvalues elas=0(1)20{
	replace netkiddeeppovrate1=`netkidpovrate_e`elas''/1000000 if _n==`elas'+1
	replace kiddeeppovchange1=`kidpovchange_e`elas'' if _n==`elas'+1

	replace lessdeeppoorkids1=`lessdeeppoorkids_e`elas'' if _n==`elas'+1
	}

* EVERYONE ELSE GETS 0.25

forvalues elas=0(1)20{
preserve

* row 1
gen double group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*(`elas'/10)*group
egen totgroup1a=sum(group1)
gen double group2=newnumkidswt17*avgtax*(`elas'/10)*group*deeppoor
egen totgroup2a=sum(group2)

gen double group3=newnumkidswt17*avgtax*(`elas'/10)*group*(deeppoor==0)
egen totgroup3a=sum(group3)

gen double group4=newnumkidswt17*avgtax*(`elas'/10)*group*(deeppoor==0)*deeppoorctc21nother
egen totgroup4a=sum(group4)

gen double group5=newnumkidswt17*avgtax*(`elas'/10)*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother
egen totgroup5a=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'a=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'a/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 2
gen double group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.25*group
egen totgroup1b=sum(group1)

gen double group2=newnumkidswt17*avgtax*.25*group*deeppoor
egen totgroup2b=sum(group2)

gen double group3=newnumkidswt17*avgtax*.25*group*(deeppoor==0)
egen totgroup3b=sum(group3)

gen double group4=newnumkidswt17*avgtax*.25*group*(deeppoor==0)*deeppoorctc21nother
egen totgroup4b=sum(group4)

gen double group5=newnumkidswt17*avgtax*.25*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother
egen totgroup5b=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'b=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'b/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 3
gen double group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.25*group
egen totgroup1c=sum(group1)

gen double group2=newnumkidswt17*avgtax*.25*group*deeppoor
egen totgroup2c=sum(group2)

gen double group3=newnumkidswt17*avgtax*.25*group*(deeppoor==0)
egen totgroup3c=sum(group3)

gen double group4=newnumkidswt17*avgtax*.25*group*(deeppoor==0)*deeppoorctc21nother
egen totgroup4c=sum(group4)

gen double group5=newnumkidswt17*avgtax*.25*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother
egen totgroup5c=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'c=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'c/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 4
gen double group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*newrtwchangespouse*.25*group
egen totgroup1d=sum(group1)

gen double group2=newnumkidswt17*newrtwchangespouse*.25*group*deeppoor
egen totgroup2d=sum(group2)

gen double group3=newnumkidswt17*newrtwchangespouse*.25*group*(deeppoor==0)
egen totgroup3d=sum(group3)

gen double group4=newnumkidswt17*newrtwchangespouse*.25*group*(deeppoor==0)*deeppoorctc21notspouse
egen totgroup4d=sum(group4)

gen double group5=newnumkidswt17*newrtwchangespouse*.25*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21notspouse
egen totgroup5d=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'd=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'd/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)

gen double group1=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25
egen totgroup1e=sum(group1)

gen double group2=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25*deeppoor
egen totgroup2e=sum(group2)

gen double group3=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25*(deeppoor==0)
egen totgroup3e=sum(group3)

gen double group4=(newnumkidswt17*person1)*avgtax*.25*(deeppoor==0)*deeppoorctc21nother+(newnumkidswt17*person2)*avgtax*.25*(deeppoor==0)*deeppoorctc21notspouse

egen totgroup4e=sum(group4)

gen double group5=(newnumkidswt17*person1)*avgtax*.25*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother+newnumkidswt17*person2*avgtax*.25*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21notspouse
egen totgroup5e=sum(group5)

gen pov1=(deeppoor*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov2=(deeppoorctc21*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov3=(deeppoorctc21*(person1*newnumkidswt17+person2*newnumkidswt17)+group4+group5)

egen pop=sum(newnumkidswt17*person1+newnumkidswt17*person2)
forvalues i=1/3{
	egen povnum`i'e=sum(pov`i')
	gen povrate`i'=povnum`i'e/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

forvalues i=1/5{
	di totgroup`i'a+totgroup`i'b+totgroup`i'c+totgroup`i'd+totgroup`i'e
}

di totkids
di totdeeppoorkids
di totstilldeeppoor
di (totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
di totdeeppoorkids/totkids
di totstilldeeppoor/totkids
di (totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)/totkids
di "pov % decrease="
di (totdeeppoorkids-(totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e))/totdeeppoorkids

local netkidpovrate_e`elas'=(totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
local kidpovchange_e`elas'=(totdeeppoorkids-(totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e))/totdeeppoorkids

local lessdeeppoorkids_e`elas'=totdeeppoorkids-(totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
di `lessdeeppoorkids_e`elas''

di `netkidpovrate_e`elas'' 
di `kidpovchange_e`elas''
restore

}

gen netkiddeeppovrate2=.
gen kiddeeppovchange2=.

gen lessdeeppoorkids2=.

forvalues elas=0(1)20{
	replace netkiddeeppovrate2=`netkidpovrate_e`elas''/1000000 if _n==`elas'+1
	replace kiddeeppovchange2=`kidpovchange_e`elas'' if _n==`elas'+1
	
	replace lessdeeppoorkids2=`lessdeeppoorkids_e`elas'' if _n==`elas'+1
}

foreach var in netkidpovrate1 netkidpovrate2 {
	replace `var'=11.177-`var'
	sum `var'
	}

foreach var in netkiddeeppovrate1 netkiddeeppovrate2 {
	replace `var'=3.11-`var'
	sum `var'
	}
	
keep elasticity netkid*povrate* kid*povchange* less*poorkids* employment*
duplicates drop	
sort elasticity

*** in 1000s
gen costpernonpoorkid1=90000000/lesspoorkids1
gen costpernonpoorkid2=90000000/lesspoorkids2
gen costpernondeeppoorkid1=90000000/lessdeeppoorkids1
gen costpernondeeppoorkid2=90000000/lessdeeppoorkids2
sum costper*

replace employment1=employment1/1000000
replace employment2=employment2/1000000
sum employment*

*** add in static estimate
expand 2 in l
replace elasticity=0 if _n==23
gen static1=0 if _n==23
gen static2=26 if _n==23	/* 90000000000/(11177000-7727000)  */	
gen static3=56 if _n==23	/* 90000000000/(3110000-1495000) */
sum static*

replace elasticity =.75 if _n==22 & elasticity==.
replace kidpovchange2=(kidpovchange2[_n-13]+kidpovchange2[_n-14])/2 if elasticity==.75
replace kiddeeppovchange2=(kiddeeppovchange2[_n-13]+kiddeeppovchange2[_n-14])/2 if elasticity==.75
replace employment2=(employment2[_n-13]+employment2[_n-14])/2 if elasticity==.75
replace costpernonpoorkid2=(costpernonpoorkid2[_n-13]+costpernonpoorkid2[_n-14])/2 if elasticity==.75
replace costpernondeeppoorkid2=(costpernondeeppoorkid2[_n-13]+costpernondeeppoorkid2[_n-14])/2 if elasticity==.75

sort elasticity

*** i have kids of parents that stop working, let's change to parents
replace employment1=employment1*(760/624)*(367/760)
replace employment2=employment2*760/624*(367/760)

twoway line costpernonpoorkid1 elasticity, c(l) pstyle(p1) || line costpernonpoorkid2 elasticity, c(l) pstyle(p2) lp(-) ytitle("Cost Per Child Pulled Out of Poverty ($1,000s)") graphregion(color(white)) bgcolor(white) xtitle("Elasticity for Unmarried EITC Mothers" "{bf:Panel A: Child Poverty}") text(25 .3 "(Left Axis)" , j(left) size(medsmall)) text(17 1.55 "(Right Axis, Employment Effects)" , j(left) size(medsmall)) xlabel(0(.4)2) || line employment1 elasticity, c(l) pstyle(p1) yaxis(2) || line employment2 elasticity, c(l) yaxis(2) pstyle(p2) lp(-) ytitle("Parents Predicted to Stop Working (Millions)", axis(2)) leg(order(1 "0.2 Elasticity for Other Moms, 0.05 for All Dads" 2 "0.25 Elasticity for Other Moms and All Dads" 10 "Static Estimates" 8 "My Preferred Estimates" 9 "Estimates from Section 7.4") c(1) pos(11) ring(0)) || scatter employment1 elasticity if (elasticity>.39 & elasticity<.41)  , pstyle(p1) m(S) yaxis(2) || scatter costpernonpoorkid1 elasticity if (elasticity>.39 & elasticity<.41)  , pstyle(p1) m(S) yaxis(1) || scatter employment2 elasticity if (elasticity>.74 & elasticity<.76) , pstyle(p2) m(S) yaxis(2) ylabel(0(.5)2.5 3 " ", axis(2)) ylabel(0(10)40 57 " ", axis(1)) || scatter static1 elasticity  , pstyle(p1) m(Oh) msize(large) yaxis(2)  || scatter static2 elasticity  , pstyle(p1) m(Oh) msize(large) || scatter costpernonpoorkid2 elasticity if (elasticity>.74 & elasticity<.76)  , pstyle(p2) m(S) yaxis(1)
graph export "$output/figure9a.png", replace

twoway line costpernondeeppoorkid1 elasticity, c(l) pstyle(p1) || line costpernondeeppoorkid2 elasticity, c(l) pstyle(p2) lp(-) ytitle("Cost Per Child Pulled Out of Deep Poverty ($1,000s)") graphregion(color(white)) bgcolor(white) xtitle("Elasticity for Unmarried EITC Mothers" "{bf:Panel B: Child Deep Poverty}") text(52 .3 "(Left Axis)" , j(left) size(medsmall)) text(34 1.52 "(Right Axis, Employment Effects)" , j(left) size(medsmall)) xlabel(0(.4)2) || line employment1 elasticity, c(l) pstyle(p1) yaxis(2) || line employment2 elasticity, c(l) yaxis(2) pstyle(p2) lp(-) ytitle("Parents Predicted to Stop Working (Millions)", axis(2)) leg(order(1 "0.2 Elasticity for Other Moms, 0.05 for All Dads" 2 "0.25 Elasticity for Other Moms and All Dads" 10 "Static Estimates" 8 "My Preferred Estimates" 9 "Estimates from Section 7.4") c(1) pos(11) ring(0)) || scatter employment1 elasticity if (elasticity>.39 & elasticity<.41)  , pstyle(p1) m(S) yaxis(2) || scatter costpernondeeppoorkid1 elasticity if (elasticity>.39 & elasticity<.41)  , pstyle(p1) m(S) yaxis(1) || scatter employment2 elasticity if (elasticity>.74 & elasticity<.76) , pstyle(p2) m(S) yaxis(2) ylabel(0(.5)2.5 2.8 " ", axis(2)) ylabel(0(20)60 107 " ", axis(1)) || scatter static1 elasticity  , pstyle(p1) m(Oh) msize(large) yaxis(2)  || scatter static3 elasticity  , pstyle(p1) m(Oh) msize(large) || scatter costpernondeeppoorkid2 elasticity if (elasticity>.74 & elasticity<.76)  , pstyle(p2) m(S) yaxis(1)
graph export "$output/figure9b.png", replace

log close